//
//  JLCLNativeViewController.m
//  JLCLTestDemo
//
//  Created by Eren on 2022/3/29.
//

#import "JLCLNativeViewController.h"

@interface JLCLNativeViewController () <JLCLNativeAdDelegate,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *sDataArray;
@property (nonatomic, strong) NSMutableArray *sourceDataArray;
@end

@implementation JLCLNativeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreen_Width, kScreen_Height - kNavBarAndStatusBarHeight) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    [self.view addSubview:self.tableView];
    if (@available(iOS 11.0, *)) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    self.sDataArray = [[NSMutableArray alloc] init];
    self.sourceDataArray = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < 30; i++) {
        NSString *text = [NSString stringWithFormat:@"JLCL feedNativeCell:%zd", (long)(i + 1)];
        [self.sourceDataArray addObject:text];
    }
    UIButton *btn = [self createBtnWithTitle:@"刷新广告" withToTop:kNavBarAndStatusBarHeight +20 +44 wihtHeight:40 withSelect:@selector(show)];
    [self.view addSubview:btn];
    [self show];
}


- (void)show {
    JLCLNativeAdData *data = [[JLCLNativeAdData alloc] init];
    data.ad_id = kJLCLTestNativeAdID;// 广告位ID   必传
    data.delegate = self;
    data.tableView = self.tableView;
    data.rootViewController = self;
    data.nativeAdFrame = CGRectMake(0, 0, self.view.bounds.size.width, 0);
    data.nativeLoadAdCount = 3;
    data.location = 1;// 位置标识符 多个同类型广告时请准确填写位置标识 默认为1
    self.aCenter = [[JLCLAdManagementCenter alloc] init];
    [self.aCenter jlcl_showNativeAd:data];

}

- (void)jlcl_nativeAdloadDidFailWithError:(NSString *)error {
    NSLog(@"广告加载失败 = \n%@",error);
}

- (void)jlcl_nativeAdExposure {
    NSLog(@"广告渲染成功");
}

- (void)jlcl_nativeAdClicked {
    NSLog(@"广告被点击");
}

- (void)jlcl_nativeAdDismiss {
    NSLog(@"广告已关闭");
}

- (void)jlcl_nativeAdClickDislike:(id)data {
    [self.sDataArray removeObject:data];
    [self.tableView reloadData];
    NSLog(@"广告被点击不喜欢");
}

- (void)jlcl_nativeAdloadSuccessWithDataArray:(NSMutableArray *)adDataArray {
    [self.sDataArray removeAllObjects];
    if(adDataArray.count > 0) {
        NSMutableArray *tempArray = [NSMutableArray arrayWithArray:self.sourceDataArray];
        NSInteger index = 0;
        for (NSInteger i = 0; i < adDataArray.count; i++) {
            NSInteger newIndex = index * 4+4;
            [tempArray insertObject:adDataArray[i] atIndex:newIndex];
            index ++;
        }
        self.sDataArray = tempArray;
        [self.tableView reloadData];

    }
    
    NSLog(@"广告加载数据源count = %ld",self.sDataArray.count);
}

- (void)jlcl_nativeVideoReady {
    NSLog(@"视频广告已就绪");
}

- (void)jlcl_nativeVideoComplete {
    NSLog(@"视频广告已结束");
}


#pragma mark - UITableViewDataSource

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  self.sDataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    id item = self.sDataArray[indexPath.row];

    UITableViewCell *cell = [self.aCenter jlcl_tableView:tableView cellForForNativeAd:item IndexPath:indexPath];
    if (cell) {
        return cell;
    } else {
        NSString *identify = NSStringFromClass(UITableViewCell.class);
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
        }
        cell.textLabel.text = [NSString stringWithFormat:@"%@",item];
        cell.textLabel.textColor = [UIColor blackColor];
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.backgroundColor = [UIColor whiteColor];
        return cell;
    }
    return  nil;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    id item = self.sDataArray[indexPath.row];
    CGFloat height =  [self.aCenter jlcl_heightForNativeAd:item];//[self.aCenter qb_heightForNativeAd:item];
    if (height > 0) {
        if (height > 55) {
        }
        
        return height;
    } else {
        return UITableViewAutomaticDimension;
    }
    return kScreen_Height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
